/*
 * Copyright 2016-2019 yoara
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package yhao.infra.common.model;

import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.util.Date;

/**
 * 所有Model类的基类
 * <p>主要是定义了ID、创建/更新人、创建/更新时间等通用字段
 * <p>实现了{@link Serializable}，{@link Comparable}
 *
 * @author yoara
 */
public class Entity<T> implements Serializable, Comparable<Entity<T>> {
    private static final long serialVersionUID = 1L;
    private T id;

    // 创建时间
    @ApiModelProperty(hidden = true)
    private Date createTime;
    // 最后更新时间
    @ApiModelProperty(hidden = true)
    private Date updateTime;
    // 创建人员
    @ApiModelProperty(hidden = true)
    private String createOperatorId;
    // 更新人员
    @ApiModelProperty(hidden = true)
    private String updateOperatorId;

    public T getId() {
        return id;
    }

    public void setId(T id) {
        this.id = id;
    }

    public int compareTo(Entity<T> o) {
        return 0;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setCreateOperatorId(String createOperatorId) {
        this.createOperatorId = createOperatorId;
    }

    public String getCreateOperatorId() {
        return createOperatorId;
    }

    public void setUpdateOperatorId(String updateOperatorId) {
        this.updateOperatorId = updateOperatorId;
    }

    public String getUpdateOperatorId() {
        return updateOperatorId;
    }

    /**
     * @param operatorId 操作人
     * @param update     为null更新全部，true更新编辑相关，false更新新增相关
     */
    public void addOperatorInfo(String operatorId, Boolean update) {
        Date now = new Date();
        if (update == null) {
            this.updateTime = now;
            this.updateOperatorId = operatorId;
            this.createOperatorId = operatorId;
            this.createTime = now;
        } else if (update) {
            this.updateTime = now;
            this.updateOperatorId = operatorId;
        } else {
            this.createOperatorId = operatorId;
            this.createTime = now;
        }
    }

    public boolean idEqual(Object obj) {
        if (!(this.getClass().equals(obj.getClass()))) {
            return false;
        }
        Entity objP = (Entity) obj;
        if (this.getId() == null || objP.getId() == null) {
            return false;
        }
        return this.getId().equals(objP.getId());
    }
}
